package com.hao.hospital.service;

import java.util.List;

import com.alibaba.druid.sql.visitor.functions.Left;
import com.hao.hospital.model.Drug;
import com.hao.hospital.model.HospitalPerson;
import com.hao.hospital.model.MedicalRecord;
import com.hao.hospital.model.OpenDrugList;
import com.hao.hospital.model.PaymentRecord;
import com.hao.hospital.model.RegisterRecord;
import com.hao.hospital.model.User;
import com.hao.hospital.util.GeneralMethod;
import com.hao.hospital.util.TimeUtil;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;
import cn.jlook.jfinal.ext3.kit.SqlBuilder.JOIN;

public class UserService extends AbstractService {
	private static UserService me;

	public static UserService me() {
		if (null == me) {
			me = Enhancer.enhance(UserService.class);
		}
		return me;
	}
	public Record userInfo(String openid){
		return SqlBuilder.from(User.table).where("openid=?").find(openid);
	}
	/**
	 * 查看个人预约记录
	 * @param openid
	 * @return
	 */
	public List<Record> register(String openid) {
		long now=System.currentTimeMillis();
		List<Record> records=SqlBuilder.from(RegisterRecord.table).field("*").alias("r").join(JOIN.LEFT, User.table, "u", "r.user_id=u.user_id")
				.join(JOIN.LEFT, HospitalPerson.table, "h", "h.person_id=r.person_id").where("openid=? and r.worktime>?").order("r.worktime desc,r.create_time desc").select(openid,now);
		for(Record r:records){
			r.set("CNworktime", TimeUtil.me().stampToDateInYearNoWeek(r.getLong("worktime").toString()));
			r.set("CNcreatetime", TimeUtil.me().stampToDateInYearNoWeek(r.getLong("create_time").toString()));
		}
		return records;
	}

	/**
	 * 完善用户信息
	 */
	public int addUserInfo(String openid, String address, String name, String paypass) {
		return SqlBuilder.from(User.table).where("openid=?", openid).update("address=?,user_name=?,paypass=?", address,
				name, paypass);
	}

	/**
	 * 用户当天是否预约过
	 * 
	 * @param openid
	 * @return
	 */
	public long isUserRegister(String openid, long worktime) {
		long today = TimeUtil.me().getDayBeginTimestamp();
		return SqlBuilder.from(RegisterRecord.table).alias("r").join(JOIN.LEFT, User.table, "u", "u.user_id=r.user_id")
				.where("openid=? and create_time>? and worktime=?").count(openid, today, worktime);
	}

	/**
	 * 通过openid获取用余额
	 * 
	 * @param openid
	 */
	public int GetUserBalance(String openid) {
		return SqlBuilder.from(User.table).where("openid=?").field("balance").find(openid).getInt("balance");
	}

	/**
	 * 通过openid支付费用
	 */
	public void DeductMoney(String openid, int money) {
		SqlBuilder.from(User.table).where("openid=?", openid).update("balance=balance-?", money);
	}

	/**
	 * 通过openid获取userid
	 * 
	 * @param openid
	 * @return
	 */
	public Record getUserIdByOpenId(String openid) {
		return SqlBuilder.from(User.table).where("openid=?").field("user_id").find(openid);
	}

	/**
	 * 通过患者openid查询用户病历
	 */
	public Record medical(String openid) {
		return SqlBuilder.from(MedicalRecord.table).alias("mr").join(JOIN.LEFT, User.table, "u", "u.user_id=mr.user_id")
				.field("*").where("u.openid=?").find(openid);
	}

	public Record verifiers(String openid) {
		System.out.println("openid查询阶段：" + openid);
		return SqlBuilder.from(User.table).where("openid=?").find(openid);
	}

	/**
	 * 通过患者id和医生id获取医生就诊过患者就诊记录
	 */
	public Record bill(int person_id, int user_id) {
		return SqlBuilder.from(MedicalRecord.table).alias("m").field("*")
				.join(JOIN.LEFT, User.table, "u", "m.user_id=u.user_id").where("m.user_id=? and m.person_id=?")
				.find(user_id, person_id);

	}

	/**
	 * 缴费管理获取到所有患者缴费信息 记录少的那张表放前面
	 */
	public Page<Record> paymentlist(int pageNumber) {
		return SqlBuilder.from(PaymentRecord.table).alias("p").join(JOIN.LEFT, User.table, "u", "u.user_id=p.user_id")
				.order("p.user_id").field("u.user_name,p.create_time,p.money").page(pageNumber, 10);
	}

	/**
	 * 缴费管理获取到所有患者未缴费信息
	 * 
	 * @return
	 */
	public Page<Record> unpaymentlist(int pageNumber) {
		return SqlBuilder.from(OpenDrugList.table).alias("o").join(JOIN.LEFT, User.table, "u", "u.user_id=o.user_id")
				.order("o.user_id").join(JOIN.LEFT, Drug.table, "d", "d.drug_id=o.drug_id")
				.field("u.user_name,d.drug_name,o.num,o.num*d.price as money,d.price").where("o.state=0")
				.page(pageNumber, 10);
	}

	/**
	 * 获取所有患者信息
	 */
	public Page<Record> allinfo(int pageNumber) {
		Page<Record> page = SqlBuilder.from(User.table).order("user_name,city").page(pageNumber, 10);
		List<Record> records = page.getList();
		for (Record r : records) {
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex")));
		}
		return page;
	}

	/**
	 * 患者对账户进行充值
	 */
	public int recharge(int user_id, int balance) {
		return SqlBuilder.from(User.table).where("user_id=?", user_id).update("balance=balance+?", balance);
	}

	/**
	 * 通过用户姓名查用户未交费内容
	 * 
	 * @param name
	 * @return
	 */
	public List<Record> unpayment(String name) {
		return SqlBuilder.from(OpenDrugList.table).alias("o").join(JOIN.LEFT, User.table, "u", "o.user_id=u.user_id")
				.where("u.user_name=? and state=0").join(JOIN.LEFT, Drug.table, "d", "o.drug_id=d.drug_id")
				.field("u.user_name,d.drug_name,o.num,d.price,o.num*d.price as money").select(name);
	}
}
